//
//  HFCalendarCollectionViewCell.m
//  HFCalendar
//
//  Created by Luffy on 2021/8/24.
//

#import "HFCalendarCollectionViewCell.h"
#import "HFCalendarModel.h"
#import "HFCalendarHeader.h"
#import "HFCalendarHelper.h"

@interface HFCalendarCollectionViewCell ()

@property (nonatomic, strong) UILabel *dayLabel;

@end

@implementation HFCalendarCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.contentView.backgroundColor = UIColor.whiteColor;
        
        self.dayLabel = [[UILabel alloc] initWithFrame:self.contentView.bounds];
        self.dayLabel.font = [UIFont systemFontOfSize:14.f];
        self.dayLabel.textAlignment = NSTextAlignmentCenter;
        [self.contentView addSubview:self.dayLabel];
    }
    return self;
}

- (void)layoutCellIndexPath:(NSIndexPath *)indexPath
                      model:(HFCalendarModel *)model {
    self.contentView.backgroundColor = UIColor.whiteColor;
    NSInteger firstDayInMonth = model.firstDayOfTheMonth;
    
    // 从每月的第一天开始设置Cell.day的值
    if (indexPath.item >= firstDayInMonth - 1
        && indexPath.item <= firstDayInMonth + model.numberOfDaysOfTheMonth - 2) {
        self.dayLabel.text = [NSString stringWithFormat:@"%ld",(long)(indexPath.item - (firstDayInMonth - 2))];
        self.userInteractionEnabled = YES;
    } else {
        self.dayLabel.text = @"";
        self.userInteractionEnabled = NO;
    }
    
    // 周末字体为红色
    if (indexPath.item % 7 == 0
        || indexPath.item % 7 == 6) {
        self.dayLabel.textColor = CustomRedColor;
    } else {
        self.dayLabel.textColor = CustomBlackColor;
    }
    
    // 今天
    HFCalendarHelper *helper = [HFCalendarHelper defaultManager];
    if ([helper whetherForTodayWithIndexPath:indexPath model:model] == HFDateEqualToToday) {
        self.dayLabel.text = @"今天";
        self.dayLabel.textColor = CustomTodayColor;
    } else if ([helper whetherForTodayWithIndexPath:indexPath model:model] == HFDateLaterThanToday) {
        // 判断日期是否超过今天
        self.dayLabel.textColor = CustomGrayColor;
        self.userInteractionEnabled = NO;
    }
}

- (void)changeContentViewBackgroundColor:(UIColor *)backgroundColor {
    self.contentView.backgroundColor = backgroundColor;
}

- (void)changeDayTextColor:(UIColor *)textColor {
    self.dayLabel.textColor = textColor;
}

@end
